﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Hkwl.WorldExpo.Merchant.Common;
using Hkwl.WorldExpo.Merchant.Model;
using Hkwl.WorldExpo.Merchant.Service;

namespace Hkwl.WorldExpo.Merchant.Web
{
    public partial class ProjectDetails : BaseCheckPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int projectId = int.Parse(Request.QueryString["ProjectId"]);
            NxProject nxProject = ServiceLocator.IProjectService.GetNxProjectById(projectId);
            this.lbProjectName.Text = nxProject.ProjectName;
            this.lbProjectDescription.Text = nxProject.ProjectDescribe;
            this.lbStartTime.Text = nxProject.StartTime.ToShortDateString();
            this.lbEndTime.Text = nxProject.EndTime.ToShortDateString();
            this.lbSendNum.Text = (ServiceLocator.ILogService.GetSendNumByProjectId(projectId)).ToString();
            this.lbUseNum.Text = (ServiceLocator.ILogService.GetUseNumByProjectId(projectId)).ToString();
            this.lbMmsTitle.Text = nxProject.MmsTitle;
            switch (nxProject.Status)
            {
                case 1: lbStatus.Text = (String)GetGlobalResourceObject("Default", "UNPUBLISHED_UNAUDITED"); break;
                case 2: lbStatus.Text = (String)GetGlobalResourceObject("Default", "PUBLISHED_UNAUDITED"); btnModify.Visible = false; break;
                case 3: lbStatus.Text = (String)GetGlobalResourceObject("Default", "SENDING"); btnModify.Visible = false; break;
                case 4: lbStatus.Text = (String)GetGlobalResourceObject("Default", "SENDED_SUCCESS"); btnModify.Visible = false; break;
                case 5: lbStatus.Text = (String)GetGlobalResourceObject("Default", "SENDED_FAILED"); btnModify.Visible = false; break;
            }
            if (nxProject.GroupId == 0)
            {
                this.lbGroup.Text = (String)GetGlobalResourceObject("Default", "ALL_CUSTOMERS");
            }
            else
            {
                this.lbGroup.Text = ServiceLocator.IClientService.GetNxGroupById(nxProject.GroupId).GroupName;
            }
            if (nxProject.Status == 4)
            {
                btnSendDetails.Visible = true;
                if (lbUseNum.Text != "0")
                {
                    btnUseDetails.Visible = true;
                }
            }
            DataBind_Mms();
        }

        protected void DataBind_Mms()
        {
            this.dlMmsList.DataSource = ServiceLocator.IProjectService.GetNxMmsCollection(int.Parse(Request.QueryString["ProjectId"]));
            
            this.dlMmsList.DataBind();
        }       

        protected void dlMmsList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                NxMms mms = (NxMms)e.Item.DataItem;

                Label lbMmsContent = (Label)e.Item.FindControl("lbMmsContent");
                if (mms.MmsPic != "")
                {
                    if (mms.MmsPosition == 1)
                    {
                        lbMmsContent.Text = "<img src='" + mms.MmsPic + "' width='100px' height='75px'/><br /><textarea id='txtMmsText' cols='20' style='overflow:auto;border:1px solid;' readonly rows='5'>" + mms.MmsText + "</textarea>";
                    }
                    else
                    {
                        lbMmsContent.Text = "<textarea id='txtMmsText' cols='20' style='overflow:auto;border:1px solid;' readonly rows='5'>" + mms.MmsText + "</textarea><br /><img src='" + mms.MmsPic + "' width='100px' height='75px'/>";
                    }
                }
                else
                {
                    lbMmsContent.Text = mms.MmsText;
                }
                Label lbMmsPage = (Label)e.Item.FindControl("lbMmsPage");
                lbMmsPage.Text = (String)GetGlobalResourceObject("Default", "THE") + mms.MmsPage.ToString() + (String)GetGlobalResourceObject("Default", "PAGE");
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            JavaScript.WriteJs("history.go(-2);", this.Page);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Response.Redirect("ModifyProject.aspx?ProjectId=" + Request.QueryString["ProjectId"]);
        }

        protected void btnSendDetails_Click(object sender, EventArgs e)
        {
            Response.Redirect("SendDetails.aspx?ProjectId=" + Request.QueryString["ProjectId"]);
        }

        protected void btnUseDetails_Click(object sender, EventArgs e)
        {
            Response.Redirect("UseDetails.aspx?ProjectId=" + Request.QueryString["ProjectId"]);
        }
    }
}
